//
//  SFPCheckAppointmentModel.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
class SFPCheckAppointmentViewModel{
    var dataSource = [SFPCheckAppointmentModel]()
    var totalInfo = ""
    var progress = 0.0
}
extension SFPCheckAppointmentViewModel{
    func getPageData(onSuccess: ((_ models: [SFPCheckAppointmentModel]) ->Void)?){
        let dataHelper = SFPCheckAppointmentModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: SFPCheckAppointmentModel.self) as! [SFPCheckAppointmentModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy.MM.dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 > date2
        }
        let all = sortedModels.count > 9 ? String(sortedModels.count) : String(format: "0%ld", sortedModels.count)
        let completedCount = sortedModels.filter { $0.completed }.count
        let complete = completedCount > 9 ? String(completedCount) : String(format: "0%ld", completedCount)
        self.totalInfo = complete + "/" + all
        let totalCount = Double(sortedModels.count)
        if totalCount != 0 {
            self.progress  = Double(completedCount) / totalCount
        } else {
            self.progress  = 0
        }
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
}

@objcMembers
class SFPCheckAppointmentModel: SFPModel,Codable {
    var date = ""
    var time = ""
    var location = ""
    var doctor = ""
    var result = ""
    var name = ""
    var completed = false
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: SFPCheckAppointmentModel.self)
    }
}
